package com.example.kaiqiang.dagger2demo

import dagger.Module
import dagger.Provides
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

@Module
class ApiModule constructor(httpLoggingInterceptor: HttpLoggingInterceptor) {
    private var httpLoggingInterceptor: HttpLoggingInterceptor = httpLoggingInterceptor

    @Provides
    fun provideOkHttpClient(): OkHttpClient {

        return OkHttpClient.Builder().addInterceptor(httpLoggingInterceptor).build()
    }

    @Provides
    fun provideApiManager(okHttpClient: OkHttpClient): MyApi.Api {
        return Retrofit.Builder().client(okHttpClient).baseUrl(MyApi.BaseUrl)
                .addConverterFactory(GsonConverterFactory.create())//添加自定义的解析
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())//添加RxJava2支持
                .build().create(MyApi.Api::class.java)

    }

//    @Provides
//    fun provideAdapter(): MyAdapter {
//        return MyAdapter(ArrayList(), context)
//    }
}